
package com.android.study;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ActivityMain extends Activity {
    ContentResolver mContentResovler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Logging.log(getClass(), "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        setButtons();
        registerBroadcast();
        registerService();
        mContentResovler = getContentResolver();
    }

    @Override
    protected void onStart() {
        Logging.log(getClass(), "onStart");
        super.onStart();
    }

    @Override
    protected void onRestart() {
        Logging.log(getClass(), "onRestart");
        super.onRestart();
    }

    @Override
    protected void onResume() {
        Logging.log(getClass(), "onResume");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Logging.log(getClass(), "onPause");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Logging.log(getClass(), "onStop");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Logging.log(getClass(), "onDestroy");
        unbindService(mConn);
        unregisterReceiver(br);
        super.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        Logging.log(getClass(), "onSaveInstanceState");
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Logging.log(getClass(), "onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
    }

    private void registerService() {
        Intent mService = new Intent(ActivityMain.this, TestService.class);
        startService(mService);
        bindService(mService, mConn, BIND_AUTO_CREATE);
    }

    private void registerBroadcast() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
        registerReceiver(br, intentFilter);
    }

    private void setButtons() {
        Button button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(buttonEvent1);
    }

    BroadcastReceiver br = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Logging.log(getClass(), "BroadcastReceiver");
        }
    };

    private ServiceConnection mConn = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
            Logging.log(getClass(), "onServiceConnected");
        }

        public void onServiceDisconnected(ComponentName name) {
            Logging.log(getClass(), "onServiceDisconnected");
        }
    };

    private OnClickListener buttonEvent1 = new OnClickListener() {
        public void onClick(View v) {
        }
    };
}
